package com.wjk.kylin.mall.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.wjk.kylin.mall.admin.entity.RoleEntity;
import com.wjk.kylin.mall.admin.entity.UserEntity;
import com.wjk.kylin.mall.admin.mapper.RoleMapper;
import com.wjk.kylin.mall.admin.service.IRoleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * <p>
 * 角色信息表 服务实现类
 * </p>
 *
 * @author wangjinkui
 * @since 2021-11-14
 */
@Service
public class RoleServiceImpl extends ServiceImpl<RoleMapper, RoleEntity> implements IRoleService {

    /**
     * 根据用户查询角色
     * @param user
     * @return
     */
    @Override
    public List<String> findRolePermissionByUser(UserEntity user) {
        List<String> roleList = new ArrayList<>();
        // 管理员拥有所有权限
        if (1 == user.getId()) {
            roleList.add("admin");
        } else {
            roleList = baseMapper.findRolePermissionByUserId(user.getId());
        }

        if(CollectionUtil.isNotEmpty(roleList)){
            roleList = roleList.stream().filter(StringUtils::isNotBlank).distinct().collect(Collectors.toList());
        }

        return roleList;
    }
}
